import { AnnotationFactory } from '@aspectjs/common';
import {  Aspect, JoinPoint, Before, on, getWeaver  } from '@aspectjs/core';

//声明一个注解
const LogBefores = new AnnotationFactory('demo').create(function LogBefores() {}, )

//定义一个切面
@Aspect()
class LogAspect {
  //声明 Joint point
  @Before(on.methods.withAnnotations(LogBefores))
  logBefore(joinPoint: JoinPoint) {
    //Advice
    console.log(`调用函数 ${joinPoint.name}，参数为 ${joinPoint.arguments}`);
  }
}

//启用 LogAspect 切面 注解
getWeaver().enable(new LogAspect());

 
class AspectjsAopShopping {
  @LogBefores()
  addToCart(item: string) {
    // 添加商品到购物车的主要逻辑
    this._doAddToCard(item);
  }

  @LogBefores()
  checkout() {
    // 结算购物车的主要逻辑
    this._doCheckout();
  }

  private _doAddToCard(item: string) {
    // 添加商品到购物车的主要逻辑
    console.log(`添加商品到购物车：${item}`);
  }

  private _doCheckout() {
    // 结算购物车的主要逻辑
    console.log('结算购物车');
  }
}

const tsShopping  = new AspectjsAopShopping() 
tsShopping.addToCart("商品A");
tsShopping.addToCart("商品B");
tsShopping.addToCart("商品C");
tsShopping.checkout();
